//
//  LGTBaseVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/7.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

//加载视图类型
enum LGTLodingType:Int{
    case NavLoadingType = 0  // 导航栏类型加载
    case TabbarLoadingType = 1 //tabbar  类型加载
    case NavAndTabbarLoadingType = 2 //导航栏 和 tabbar
    case NoNavAndTabbarLoadingType = 3 //都没有
    //case HaveAllLoadingType = 3 //所有都有
}

class LGTBaseVC: UIViewController {
    var reloadBlock:ClickBlock?
    let navBar:UIView = {
        return UIView.init(frame: CGRect.init(x: 0.0, y: 0.0, width: Double(KScreenW), height: NavBarH()))
    }()
    
    let titleH:CGFloat = 17
    var navTitleLabel:UILabel?
    var backBtn:LGTButton?
    var noView:UIView?
    var noImage:UIImageView?
    var noLabel:UILabel?
    var noSubLabel:UILabel?
    var loadinView:LGTLodingView?
    var navLine:UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
    }
    
    func setUI(){
        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.isHidden = true
        //self.hidesBottomBarWhenPushed = true
        //self.setBavBarStyle(navBarHiden: false, title: "hello", back: true, titleColor: NavTitleColor)
    }
    
    func setBavBarStyle(navBarHiden:Bool, title:String = "", back:Bool = false, backColor:UIColor = UIColor.white, titleColor:UIColor = NavTitleColor) -> Void{
        
        if navBarHiden == true{
            self.navigationController?.isNavigationBarHidden = true
            return
        }
        
        self.view.addSubview(self.navBar)
        self.navBar.isUserInteractionEnabled = true
        self.navTitleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: NavTitleColor, fontSize:self.titleH,  text: title)
        let w:CGFloat = SizeFitFloat(220);
        self.navTitleLabel!.frame = CGRect.init(x: (KScreenW - w)/2, y: CGFloat(IPHONENAV_DIS() + 34.0), width: w, height: self.titleH)
        self.navTitleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
        self.view.addSubview(self.navTitleLabel!)
        
        self.navLine = UIView.init(frame: CGRect.init(x: 0.0, y: NavBarH() - 1.0, width: Double(KScreenW), height: 0.5))
        // da
        self.navLine?.backgroundColor = BaseUtil.baseUtil.getHexColor(colorHex: 0xdadada)
        self.navBar.addSubview(self.navLine!)
        
        if back == false{
            return
        }
        
        self.backBtn = LGTViewCreateUtil.share.createLGTButton(image: "return_icon", selected: "return_icon", dW: 10, dH: 10)
        self.backBtn!.frame = CGRect.init(x: 14.0, y: Double(IPHONENAV_DIS() + 34.0), width: 20.0, height: 17.0)
        self.backBtn!.addTarget(self, action: #selector(backAction), for: UIControl.Event.touchUpInside)
        self.navBar.addSubview(self.backBtn!)
    }
    
    @objc func backAction(){
        
       
//        var dict: Dictionary<Int, String> = [1 : "a", 2 : "b"]
//        var dict1 = dict
//        dict1[3] = "c"
//        print(dict1)
//        print(dict)
        if self.navigationController == nil{
            self.dismiss(animated: false, completion: nil)
        }
        
        if self.navigationController?.viewControllers.count == 1{
            return;
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    //networkanomaly
    func showNoData(image:String, text:String, subText:String, backColor:UIColor = UIColor.white, haveNav:Bool = true){
        if self.noView == nil{
            self.noView = UIView.init()
            self.noView?.backgroundColor = backColor
            self.view.addSubview(self.noView!)
            self.noImage = LGTViewCreateUtil.share.createImageView(image: image)
            self.noView?.addSubview(self.noImage!)
            self.noLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: CGFloat(SizeFit(15)), text: text)
            self.noView?.addSubview(self.noLabel!)
            
            let topH = haveNav == true ? NavBarH():0
            self.noView?.snp.makeConstraints({ (make) in
                make.top.equalTo(topH)
                make.left.right.bottom.equalTo(0)
            })
            
            self.noImage?.snp.makeConstraints({ (make) in
                make.width.equalTo(SizeFit(100))
                make.height.equalTo(SizeFit(115))
                make.centerX.equalTo(self.noView!)
                make.centerY.equalTo(self.noView!).offset(-SizeFit(50));
            })
            
            self.noLabel?.snp.makeConstraints({ (make) in
                make.top.equalTo(self.noImage!.snp_bottomMargin).offset(SizeFit(20))
                make.centerX.equalTo(self.noView!)
                make.height.equalTo(SizeFit(16))
            })
            
            self.noSubLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: CGFloat(SizeFit(12)), text: subText)
            self.noView?.addSubview(self.noSubLabel!)
            self.noSubLabel?.snp.makeConstraints({ (make) in
                make.top.equalTo(self.noLabel!.snp_bottomMargin).offset(SizeFit(10))
                make.centerX.equalTo(self.noView!)
                make.height.equalTo(SizeFit(14))
            })
            
            self.noView?.isUserInteractionEnabled = true
            let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(npDataTapAction))
            self.noView?.addGestureRecognizer(tap)
        }
        
        self.noView?.isHidden = false
        self.view.bringSubviewToFront(self.noView!)
    }
    
    @objc func npDataTapAction(){
        self.noView?.isHidden = true
        if self.reloadBlock != nil{
            self.reloadBlock!()
        }
    }
    
    //显示弹窗
    func showAlerView(title:String, desc:String, subText:String, sureTitle:String="确认", cancelTitle:String="取消", sureBlock:@escaping ClickBlock, cancelBlock:@escaping ClickBlock){
        LGTRWZCommenAlert.CommenAlertalertShow(title: title, desc: desc, subText: subText, sureTitle: sureTitle, cancelTitle: cancelTitle, sureBlock:sureBlock, cancelBlock: cancelBlock, currentVC: self.navigationController!)
    }
    
    //显示加载
    func showLoading(loadingType:LGTLodingType){
        var navH = NavBarH()
        var tabbarH = TabBarH()
        if loadingType == .TabbarLoadingType{
            navH = 0
        }
        
        if loadingType == .NavLoadingType{
            tabbarH = 0
        }
        
        if loadingType == .NoNavAndTabbarLoadingType{
            navH = 0
            tabbarH = 0
        }
        
        if self.loadinView == nil{
            self.loadinView = LGTLodingView()
            self.loadinView?.backgroundColor = UIColor.white
            self.view.addSubview(self.loadinView!)
            self.loadinView?.snp.makeConstraints({ (make) in
                make.top.equalTo(navH)
                make.left.equalToSuperview()
                make.width.equalToSuperview()
                make.bottom.equalTo(-tabbarH)
            })
        }
        self.loadinView?.showLoading()
    }
    
    //关闭加载
    func closeLoading(){
        if self.loadinView != nil{
            self.loadinView?.closeLoading()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}
